﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace memcached.Tx
{
    /// <summary>
    /// We assume null is not a valid value.
    /// </summary>
    internal sealed class RemoveOp : KeyedOp
    {
        [System.Diagnostics.DebuggerHidden]
        public RemoveOp(ICache real, ICache temp, string key)
            : base(real, temp, key)
        {
        }

        private object Original;
        private bool Done;

        public override string Name
        {
            [System.Diagnostics.DebuggerHidden]
            get { return "Remove:" + Key; }
        }

        public override object Execute()
        {
            return Temp.Remove(Key);
        }

        public override bool Persist()
        {
            bool res;
            Original = Store.Get(Key, out res);
            if (res)
                res = Store.Remove(Key);
            Done = res;
            return res;
        }

        public override bool Rollback()
        {
            // we need to know if we really made the change or not
            if (Done && Original != null)
                return Store.Set(Key, Original);// we loose any expiry information
            return true;
        }
    }
}
